<template>
  <div>
    <el-form
    ref="ruleFormRef"
    style="max-width: 600px"
    :model="ruleForm"
    status-icon
    :rules="rules"
    label-width="auto"
    class="demo-ruleForm"
  >
    <el-form-item label="审核状态" :prop="statusColumn">
      <el-select v-model="ruleForm[statusColumn]" placeholder="请选择审核状态" >
        <el-option v-for="status in statuses" :label="status.label" :value="status.value"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="审核状态" :prop="reasonColumn">
        <el-input v-model="ruleForm[reasonColumn]" type="textarea" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        提交
      </el-button>
    </el-form-item>
  </el-form>
  <el-table :data="authLogs" style="width: 100%">
    <el-table-column :prop="statusColumn + '_text'" label="审核状态" width="180" />
    <el-table-column :prop="reasonColumn" label="审核缘由" width="180" />
    <el-table-column prop="create_time" label="审核时间" />
  </el-table>
  </div>
</template>

<script lang="ts" setup>
import { ref, defineEmits } from 'vue'
import type { FormInstance, FormRules, FormValidateCallback } from 'element-plus'
import { getAuthStatus, submitAuth } from "@/api/resources"
type KeyValue = {
  label: string
  value: any
};
const ruleForm = ref<any>({})
const props = defineProps(["model",'id'])    // 获取表单数据
const emits = defineEmits(['closeForm'])
const ruleFormRef = ref<FormInstance>()
const statuses = ref<KeyValue[]>([])
const statusColumn = ref('')
const reasonColumn = ref('')
const authLogs = ref([])
const rules = ref<FormRules>({})
getAuthStatus(props.model, props.id).then((res: any) => {
    console.log(res.data)
    statuses.value = res.data.auth.values
    statusColumn.value = res.data.auth.field
    reasonColumn.value = res.data.reason.field
    rules.value[statusColumn.value] = [{ required: true, message: '请选择审核状态', trigger: 'blur' }]
    authLogs.value = res.ext.logs
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid, fields) => {
    if (valid) {
        submitAuth(props.model, props.id, ruleForm.value).then((res: any) => {
            console.log(res.data)
        })
      console.log('submit!')
    } else {
      console.log('error submit!')
      return
    }
  })

emits("closeForm")
}
</script>
